package com.yanqu.road.dao.impl.drugstore;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.drugstore.UserDrugChuFangData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class UserDrugChuFangDataDaoImpl extends TempDataObjectDao<UserDrugChuFangData> {

    public static String getUserDrugChuFangDataSql = "select * from `t_u_drug_chu_fang` where `userId`=?";
    public Map<Integer, UserDrugChuFangData> getUserDrugChuFangData(long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer, UserDrugChuFangData> result = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getUserDrugChuFangDataSql);
            int i = 1;
            call.setLong(i++, userId);
            rs = call.executeQuery();
            while (rs.next()) {
                UserDrugChuFangData data = parse(rs);
                result.put(data.getChuFang(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return result;
    }

    public static String insertUserDrugChuFangDataSql = "insert into `t_u_drug_chu_fang`(userId,chuFang,lv,shuLianDu) values (?,?,?,?)";

    @Override
    protected boolean addData(UserDrugChuFangData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getChuFang()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getLv()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getShuLianDu()));
        if (getDb().executeNoneQuery(insertUserDrugChuFangDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String updateUserDrugChuFangDataSql = "update `t_u_drug_chu_fang` set lv=?,shuLianDu=? where userId=? and chuFang=?";

    @Override
    protected boolean updateData(UserDrugChuFangData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, data.getLv()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getShuLianDu()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getChuFang()));
        if (getDb().executeNoneQuery(updateUserDrugChuFangDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected UserDrugChuFangData parseData(ResultSet rs) throws SQLException {
        UserDrugChuFangData data = new UserDrugChuFangData();
        data.setUserId(rs.getLong("userId"));
        data.setChuFang(rs.getInt("chuFang"));
        data.setLv(rs.getInt("lv"));
        data.setShuLianDu(rs.getLong("shuLianDu"));
        return data;
    }
}
